<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Beidou extends Base
{
    public function index() {
        $driver_id = input('param.driver_id');
        if($driver_id){
            $order_return = Db::name('order')->where('driver_id',$driver_id)->find();
            if($order_return['car_number']) {
                header("location:/admin/beidou/index?number=".$order_return['car_number']);
            }
            else{
                $this->error('该司机最近无订单');
            }
        }
        return $this->out();
    }

    //所有车辆
    public function cars() {

        $json = $this->beidouApi();
        if($json['code'] == 1) {
//			
            $data = array_column($json['data'],null,'driver_id');
            $data = array_column($json['data'],null,'carno');
            #$ids = array_column($json['data'],'carno');
            //
            $where = [];
            $where['is_beidou'] = 1;
            $number = input('param.number','','urldecode');
            $car_rental_id = input('param.car_rental_id');
            $number && $where['number'] = ['like',"%{$number}%"];
            $car_rental_id && $where['car_rental_id'] = ['like',"%{$car_rental_id}%"];

            //数据库
            #$list = Db::name('car')->alias('c')->join('driver m','m.car_id = c.car_id','left')->field('m.driver_id,c.car_id,m.name,c.number,m.phone,m.work_state')->where($where)->select();
            $list = Db::name('car')->field('car_id,number')->where($where)->select();

            foreach($list AS $id => $value) {
                $beidou = $data[$value['number']];
                $value['loc'] = [$beidou['lon'],$beidou['lat']];
                $states = explode(' ',trim($beidou['state']));
                switch($states[0]) {
                    case '停止':
                        $value['state'] = 1;
                        break;
                    case '行驶':
                        $value['state'] = 2;
                        break;
                    case '':
                        $value['state'] = 4;
                        break;
                }
                //离线
                if($states[1] == '离线') $value['state'] = 3;
                //预警
                $value['alarm'] = $beidou['alarm'];
                $list[$id] = $value;
            }
//			halt($list);
            return $this->success('数据请求成功。','',$list);

        } else {
            return $this->error('数据请求异常，请重新发起请求。');
        }
    }
    //预警
    public function alarm() {

        $json = $this->beidouApi();
        $alarm = [];

        foreach($json['data'] AS $data) {
            if($data['alarm']) {
                $data['number'] = $data['carno'];
                $data['time'] = date('Y-m-d H:i',$data['time']);
                $alarm[] = $data;
            }
        }
        if(request()->isAjax()) {
            return $alarm;
        }

        $this->assign('list',$alarm);

        return $this->popup();
    }

    protected function beidouApi() {

        $json = $this->CF('http://new.jjcxfw.com/api/driver.index/getredisbeidou',['act'=>'beidou'])[0];
        $json = json_decode($json,1);

        return $json;

    }

    //车辆查订单
    public function orders() {

        $id = input('param.id/d');
        $page = input('param.page/d',1);
        $size = input('param.pagesize/d');

        $list = Db::name('order')->alias('o')
            ->join('order_fee of','of.order_id = o.order_id')
            ->join('order_gps og','og.order_id = o.order_id')
            ->join('driver d','d.driver_id = o.driver_id')
            ->where('o.car_id',$id)->field('o.*,d.name AS driver_name,of.order_money,og.from_lon,og.from_lat')->page($page)->limit($size)->order('order_time DESC')->select();

        if($list) {
            return $this->success('数据请求成功。','',$list);
        }
        return $this->error('数据请求异常，请重新发起请求。');

    }

    public function getDate() {

        $act = input('param.act');

        $number = input('param.number');
        $start = input('param.start');
        $end = input('param.end');

        $md5 = md5($number);
        //时间段
        $starttime = strtotime($start);
        $endtime = strtotime($end);
        $diff =  max(0,($endtime-$starttime)/(24*3600),10);

        $beidou = $lines = $content = [];
        for($i = 0;$i <= $diff; $i++) {
            $date = date('Ymd',$starttime+$i*24*3600);
            $file = "/work/ucar/beidou/{$md5}/{$date}*.txt";

            $list = glob($file);
            if($list) {
                foreach($list AS $file) {
                    $lines = file($file);
                    if($lines){
                        foreach($lines AS $line) {
                            if($info = explode('|',$line)) {
                                $beidou[] = [$info[4],$info[5]];
                                $content[] = [$number,$info[11],$info[4],$info[5],$info[6],floatval($info[12]),$info[8],$info[9]];
                            }
                        }
                    }

                }
            }
        }

        if($act == 'export') {
            $title = ['车牌号','时间','经度','纬度','速度','里程','相对里程','车辆状态'];
            return $this->xls($number.'_'.$start.' '.$end.' Beidou',$title,$content);
        } else {
            return $this->apiout($beidou);
        }

    }

    //查看司机当前位置
    public function lookWhere(){
        $driver_id = input('param.driver_id');
        $driver_return = Db::name('driver')->where('driver_id',$driver_id)->find();

        $this->assign('notPopup',0);//不需要侧边栏
        $this->assign('number',$driver_return['car_number']);
        $this->assign('loc','['.$driver_return['lon'].','.$driver_return['lat'].']');
        return $this->out();
    }

}
